//
//  ActivityViewController.m
//  豆瓣电影
//
//  Created by dllo on 16/6/23.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ActivityViewController.h"
#import "ActivityModel.h"
#import "OwnerModel.h"
#import "ActivityTableViewCell.h"

@interface ActivityViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, retain) NSMutableArray *arrayOfActivity;

@property (nonatomic, retain) NSMutableArray *arrayOfOwner;

@property (nonatomic, retain) UITableView *tableView;

@end

@implementation ActivityViewController

-(void)dealloc
{
    [_tableView release];
    [self.arrayOfActivity release];
    [self.arrayOfOwner release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self config];
    
    [self handleData];
    
    [self createTableView];

}

-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.41 green:0.61 blue:0.68 alpha:1.0];
}

-(void)config
{
    self.arrayOfOwner = [NSMutableArray array];
    
    self.arrayOfActivity = [NSMutableArray array];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.navigationItem.title = @"活动";
    
    self.view.backgroundColor = [UIColor whiteColor];
}

#pragma mark - 解析数据
-(void)handleData
{
    NSString *path = [[NSBundle mainBundle]pathForResource:@"activitylist" ofType:@"txt"];
    
    NSData *data = [NSData dataWithContentsOfFile:path];
    
    NSError *error = nil;
    
    NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
    
    NSArray *array = [dictionary objectForKey:@"events"];
    
    for (NSDictionary *dic in array) {
        
        ActivityModel *activityModel = [[ActivityModel alloc]init];
        
        [activityModel setValuesForKeysWithDictionary:dic];
        
        [self.arrayOfActivity addObject:activityModel];
     }
    
//    NSLog(@"%@", self.arrayOfActivity);
    
}

#pragma mark - tableView
-(void)createTableView
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 110) style:UITableViewStylePlain];
    
    self.tableView.dataSource = self;
    
    self.tableView.delegate = self;
    
    [self.view addSubview:self.tableView];
    
    self.tableView.rowHeight = 500;
    
    [self.tableView registerClass:[ActivityTableViewCell class] forCellReuseIdentifier:@"cell"];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arrayOfActivity.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 200;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ActivityTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    ActivityModel *activityModel = [self.arrayOfActivity objectAtIndex:indexPath.row];
    
    cell.cellActivityModel = activityModel;

    return cell;
}

@end



















